import axios from "axios";

import {
  ADD_NUMBER,
  SUB_NUMBER,
  SET_BANNER,
  SET_RECOMMEND,
  FETCH_HOME_MULTIDATA
} from './constants.js'

export const addAction = num => ({
  type: ADD_NUMBER,
  num
})

export const subAction = num => ({
  type: SUB_NUMBER,
  num
})

//banner and recommend
export const changeBannerAction = list => ({
  type: SET_BANNER,
  list
})
export const changeRecommendAction = list => ({
  type: SET_RECOMMEND,
  list
})

//redux-thunk中定义的action函数       //自动传入一个 dispatch&redux里的state
export const changeHomeMultiAction = (dispath, getState) => {
  axios({
    url: "http://123.207.32.32:8000/home/multidata"
  }).then(res => {
    const { data } = res.data;
    console.log("redux-thunk", data);
    dispath(changeBannerAction(data.banner.list))
    dispath(changeRecommendAction(data.recommend.list))
  })
}

//redux-saga拦截action
export const fetchHomeMultidataAction = {
  type: FETCH_HOME_MULTIDATA
}